var map;
var colorList = ["#FF0000","#FF8000","#FFE100","#D5FF00","#88FF00","#00FFBB","#00D0FF","#0099FF","#8D70FF","#C670FF","#FE71FE","#B081EE","#2C8C6C"]
var is_start = false
var is_stopped = false
var options = {
	icon: new BMap.Symbol(BMap_Symbol_SHAPE_STAR,{
		scale:0.5,
		rotation:0,
		fillColor:"red",
		strokeColor:"red",
		fillOpacity:1
	})
}
var options2 = {
	icon: new BMap.Symbol(BMap_Symbol_SHAPE_STAR,{
		scale:0.5,
		rotation:0,
		fillColor:"orange",
		strokeColor:"orange",
		fillOpacity:1
	})
}
//地图初始化
function initMap(){
	map = new BMap.Map("main",{enableMapClick:true});
	var startPoint = {x:109.062603,y: 32.409127};
	var point = new BMap.Point(startPoint.x, startPoint.y);
	var mapStyle = {
		features: ["road", "building", "water", "land"],
		//隐藏地图上的poi
		style: "midnight" //设置地图风格为高端黑
	}
	map.centerAndZoom(point, 6);
	map.enableScrollWheelZoom(true);
	map.enableDragging(true);
	map.setDefaultCursor("crosshair");
	map.setMapStyle(mapStyle);
}
//跳至相关地区中心点并生成边界
function button_jump(){
	var name = $("#input_area").val()
	if(name.length == 0){
		alert("位置名称不能为空")
	}else{
		map.centerAndZoom(name,10);	
		var bdary = new BMap.Boundary();
		bdary.get(name, function(rs) { //获取行政区域  
			var count = rs.boundaries.length; //行政区域的点有多少个
			for (var i = 0; i < count; i++) {
				var ply = new BMap.Polygon(rs.boundaries[i], {
					strokeWeight: 2,
					strokeColor: "#FFFFFF",
					fillColor: "#1c7099",
					strokeStyle:"normal"
				}); //建立多边形覆盖物
				ply.setFillOpacity(0.01);
				//alert(ply.getPath())
				map.addOverlay(ply); //添加覆盖物
			}
		});
	}
}
//标点方法
var clickHandler = function(e){
	var point = new BMap.Point(e.point.lng,e.point.lat)
	var marker = new BMap.Marker(point,options);
	marker.addEventListener("click",function(e){//marker设置监听 点击即删除
		var p = e.target
		map.removeOverlay(p)
	})
	map.addOverlay(marker);
}
//开始标点
function button_setPoints(){
	is_start = true
	is_stopped = false
	$("#b_start").attr("disabled", is_start)
	$("#b_stop").attr("disabled", is_stopped)
	$("#b_createPoly").attr("disabled", is_start)
	$("#b_createPoints").attr("disabled", is_start)
	map.addEventListener("click",clickHandler );
}
//停止标点
function button_stopSettingPoints(){
	is_start = false
	is_stopped = true
	$("#b_start").attr("disabled", is_start)
	$("#b_stop").attr("disabled", is_stopped)
	$("#b_createPoly").attr("disabled", is_start)
	$("#b_createPoints").attr("disabled", is_start)
	map.removeEventListener("click",clickHandler)	
}
//清空地图
function button_clearAll(){
	is_start = false
	is_stopped = false
	var arrayOverlays = map.getOverlays();
	for(var i=0;i<arrayOverlays.length;i++){
		map.removeOverlay(arrayOverlays[i])
	}
}
//生成点数据
function button_generatePointsData(){
	$("#pointsData").val("")
	var pointType = $("#select_pointType").val()//标注点生成类型
	var overlays = map.getOverlays();
	var mapPoints = ""
	var j = 1;
	for(var i=0;i<overlays.length;i++){
		if (overlays[i].toString() == "[object Marker]") {
			if(pointType == "bmap"){
				mapPoints += "new BMap.Point("+overlays[i].point.lng+","+overlays[i].point.lat+"),\n"
			}else if(pointType == "geo"){
				mapPoints += "'"+j+"':["+overlays[i].point.lng+","+overlays[i].point.lat+"],\n"	
			}else{
				mapPoints += "["+overlays[i].point.lng+","+overlays[i].point.lat+"],\n"	
			}
			j++;
		}
	}
	//mapPoints += "\n标注点生成完毕，复制数据请删除本行"
	$("#pointsData").val(mapPoints)
}
//根据已有点数据解析生成地图标注点
function button_createPoints(){
	var pointType = $("#select_pointType").val()//点数据类型
	var pointsStr = $("#pointsData").val()//获取的文本域内容
	//try { 
		if(pointsStr.length != 0){
			var points = new Array();
			if(pointType == "bmap"){//百度地图点格式解析
				var tempStr = "["+pointsStr+"]"
				points = eval(tempStr)
				if(points.length != 0){
					for(var i=0;i<points.length;i++){
						var marker = new BMap.Marker(points[i],options2);
						marker.addEventListener("click",function(e){//marker设置监听 点击即删除
							var p = e.target
							map.removeOverlay(p)
						})
						map.addOverlay(marker);
					}
				}
			}else if(pointType == "geo"){//geocoord点格式解析
				//"1":[x,y "2":[x,y 
				
				if(pointsStr.substring(pointsStr.length-1,pointsStr.length) != ","){
					pointsStr += ","
				}
				var tempArr = new Array();
				tempArr = pointsStr.split("],")	//变成 "1":[x1,y1  "2":[x2,y2  格式
				if(tempArr.length != 0){
					for(var i=0;i<tempArr.length-1;i++){
						var tempArr2 = tempArr[i].split(":[")//变成 "1"  :[  x1,y1  格式
						if(tempArr2.length != 0){
							var tempArr3 = tempArr2[1].split(",")//变成 x1  y1 格式
							var tempPoint = new BMap.Point(tempArr3[0],tempArr3[1])
							var marker = new BMap.Marker(tempPoint,options2);
							marker.addEventListener("click",function(e){//marker设置监听 点击即删除
								var p = e.target
								map.removeOverlay(p)
							})
							map.addOverlay(marker);
						}
					}	
				}
			}else{//一般点格式解析
				if(pointsStr.substring(pointsStr.length-1,pointsStr.length) != ","){
					pointsStr += ","
				}
				var tempArr = new Array();
				tempArr = pointsStr.split("],");	//变成 [x1,y1 [x2,y2 格式
				if(tempArr.length != 0){
					var tempArr2 = new Array();
					for(var i=0;i<tempArr.length-1;i++){
						tempArr2.push(tempArr[i].replace("[","").replace("]",""))	//变成 x1,y1 x2,y2 格式
					}
					if(tempArr2.length != 0){
						for(var i=0;i<tempArr2.length;i++){
							var tempArr3 = tempArr2[i].split(",")//变成 x1  y1 格式
							var tempPoint = new BMap.Point(tempArr3[0],tempArr3[1])
							var marker = new BMap.Marker(tempPoint,options2);
							marker.addEventListener("click",function(e){//marker设置监听 点击即删除
								var p = e.target
								map.removeOverlay(p)
							})
							map.addOverlay(marker);
						}
					}
				}
			}
		}else{
			alert("点数据不能为空")	
		}
	//}catch (e){ 
		//alert(e.message); 
		//alert(e.description) 
		//alert(e.number) 
		//alert(e.name) 
	//} 

}
//根据js文件创建标注点
function button_createPointsByJs(){
	for(var i=0;i<pointsArray.length;i++){
		var marker = new BMap.Marker(pointsArray[i],options);
		marker.addEventListener("click",function(e){//marker设置监听 点击即删除
			var p = e.target
			map.removeOverlay(p)
		})
		map.addOverlay(marker);
	}
}

//创建边界
function button_createBoundary(){
	var tempArray = new Array()
	var overlays = map.getOverlays();
	
	for(var i=0;i<overlays.length;i++){
		if (overlays[i].toString() == "[object Marker]") {
			tempArray.push(new BMap.Point(overlays[i].getPosition().lng,overlays[i].getPosition().lat))
		}
	}
	var poly = new BMap.Polygon(tempArray)
	poly.setStrokeWeight(1);
	poly.setFillOpacity(0.3)
	poly.setFillColor(colorList[RandomNumBoth(1,13)-1])
	poly.setStrokeColor("white");
	map.addOverlay(poly);
}

function RandomNumBoth(Min,Max){
      var Range = Max - Min;
      var Rand = Math.random();
      var num = Min + Math.round(Rand * Range); //四舍五入
      return num;
}

initMap()